/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import com.sun.lwuit.Font;
import datastorage.RMSManager;

/**
 *
 * @author Kvest
 */
public class Settings 
{
    public static final String THEME_DAY = "day_theme";
    public static final String THEME_NIGHT = "night_theme";
    
    private static final String RMS_NAME = "settings";
    private static final String CALLSIGN_KEY = "call_sign";
    private static final String LOGIN_KEY = "login";
    private static final String PASSWORD_KEY = "pass";
    private static final String SERVER_IP_KEY = "server_ip";
    private static final String EXTRA_SERVER_IP_KEY = "extra_server_ip";
    private static final String FONT_SIZE_KEY = "font_size";
    private static final String FONT_BOLD_KEY = "font_bold";
    private static final String EFIR_SORT_KEY = "efir_sort";
    private static final String DISP_TIME_KEY = "dist_time";
    private static final String EFIRE_TIME_KEY = "efire_time";
    private static final String THEME_KEY = "theme";
    private static final String PREORDERS_UPDATE_PERIOD_KEY = "preord_upd_per";
    private static final String MAX_ARCHIV_ORDES_COUNT = "maoc";

    public static final int ES_BYTIME = 0;
    public static final int ES_ALIEN_BOTTOM = 1;
    
    private static Settings settings = null;;
    private static final Object lock = new Object();
    
    private String callsign;
    private String login;
    private String password;
    private String serverIP;
    private String extraServerIP;
    
    private int fontSize;
    private boolean fontBold;
    private String theme;
    
    private int efirSort;
    
    private int dispTime; 
    private int efirTime;
    
    private int volume; 
    private boolean vibration;
    
    private int preordersUpdatePeriod; 
    private int maxArchivOrdersCount;
    
    private RMSManager rmsManager;
    
    public static Settings getSettings()
    {
        synchronized(lock)
        {
            if (settings == null)
            {
                settings = new Settings();
            }
        }
        
        return settings;
    }
    
    private Settings()
    {   
        rmsManager = new RMSManager(RMS_NAME);
        
        callsign = "";
        login = "";
        password = "";
        serverIP = IMAPMIDlet.getMidlet().getServerIp();
        extraServerIP = IMAPMIDlet.getMidlet().getExtraServerIp();
        
        fontSize = Font.SIZE_MEDIUM;
        fontBold = false;
        theme = THEME_DAY;
        
        efirSort = ES_BYTIME;
        
        dispTime = 10;
        efirTime = 10;
        
        volume = 100;
        vibration = true;
        
        preordersUpdatePeriod = 0;
        maxArchivOrdersCount = 20;
        
        //ToDo Kvest 
        //delete
//        if ("wtk-emulator".equals(System.getProperty("device.model")))
//        {
//            callsign = "5101";
//            login = "5101";
//            password = "5101";
//            serverIP = "194.48.212.11:6000";//"194.0.150.90:6001";
//            extraServerIP = "194.48.212.11:6000";
//        }
        
        load();
    }
    
    public void save()
    {
        rmsManager.set(CALLSIGN_KEY, callsign);
        rmsManager.set(LOGIN_KEY, login);
        rmsManager.set(PASSWORD_KEY, password);
        rmsManager.set(SERVER_IP_KEY, serverIP);
        rmsManager.set(EXTRA_SERVER_IP_KEY, extraServerIP);
        
        rmsManager.set(FONT_SIZE_KEY, fontSize);
        rmsManager.set(FONT_BOLD_KEY, fontBold);
        rmsManager.set(THEME_KEY, theme);
        
        rmsManager.set(EFIR_SORT_KEY, efirSort);
        
        rmsManager.set(DISP_TIME_KEY, dispTime);
        rmsManager.set(EFIRE_TIME_KEY, efirTime);
        
        rmsManager.set(PREORDERS_UPDATE_PERIOD_KEY, preordersUpdatePeriod);
        rmsManager.set(MAX_ARCHIV_ORDES_COUNT, maxArchivOrdersCount);
        
        rmsManager.save();
    }
    
    public final void load()
    {
        callsign = rmsManager.get(CALLSIGN_KEY, callsign);
        login = rmsManager.get(LOGIN_KEY, login);
        password = rmsManager.get(PASSWORD_KEY, password);
        serverIP = rmsManager.get(SERVER_IP_KEY, serverIP);
        extraServerIP = rmsManager.get(EXTRA_SERVER_IP_KEY, extraServerIP);
        
        fontSize = rmsManager.getInt(FONT_SIZE_KEY, fontSize);
        fontBold = rmsManager.getBool(FONT_BOLD_KEY, fontBold);
        theme = rmsManager.get(THEME_KEY, theme);
        
        efirSort = rmsManager.getInt(EFIR_SORT_KEY, efirSort);
        
        dispTime = rmsManager.getInt(DISP_TIME_KEY, dispTime);
        efirTime = rmsManager.getInt(EFIRE_TIME_KEY, efirTime);
        
        preordersUpdatePeriod = rmsManager.getInt(PREORDERS_UPDATE_PERIOD_KEY, preordersUpdatePeriod);
        maxArchivOrdersCount = rmsManager.getInt(MAX_ARCHIV_ORDES_COUNT, maxArchivOrdersCount);
    }
    
    public String getTheme()
    {
        return theme; 
    }

    public void setTheme(String theme) 
    {
        this.theme = theme;
    }

    public String getCallsign() 
    {
        return callsign;
    }

    public void setCallsign(String callsign) 
    {
        this.callsign = callsign;
    }

    public String getLogin() 
    {
        return login;
    }

    public void setLogin(String login) 
    {
        this.login = login;
    }

    public String getPassword() 
    {
        return password;
    }

    public void setPassword(String password) 
    {
        this.password = password;
    }

    public boolean hasAuthData()
    {
        return (!callsign.equals("") && !login.equals("") && !password.equals("")); 
    }

    public void setServerIP(String serverIP) 
    {
        this.serverIP = serverIP;
    }
    
    public String getServerIP()
    {
        //ToDo Kvest
        return serverIP;
    }

    public String getExtraServerIP() {
        return extraServerIP;
    }

    public void setExtraServerIP(String extraServerIP) {
        this.extraServerIP = extraServerIP;
    }
    
    public boolean isFontBold() 
    {
        return fontBold;
    }

    public void setFontBold(boolean fontBold) 
    {
        this.fontBold = fontBold;
    }

    public int getFontSize() 
    {
        return fontSize;
    }

    public void setFontSize(int fontSize) 
    {
        this.fontSize = fontSize;
    }

    public int getEfirSort() 
    {
        return efirSort;
    }

    public void setEfirSort(int efirSort) 
    {
        this.efirSort = efirSort;
    }

    public int getDispTime() 
    {
        return dispTime;
    }

    public void setDispTime(int dispTime) 
    {
        this.dispTime = dispTime;
    }

    public int getEfirTime() 
    {
        return efirTime;
    }

    public void setEfirTime(int efirTime) 
    {
        this.efirTime = efirTime;
    }

    public boolean isVibration() 
    {
        return vibration;
    }

    public void setVibration(boolean vibration) 
    {
        this.vibration = vibration;
    }

    public int getVolume() 
    {
        return volume;
    }

    public void setVolume(int volume) 
    {
        this.volume = volume;
    }

    public int getPreordersUpdatePeriod() 
    {
        return preordersUpdatePeriod;
    }

    public void setPreordersUpdatePeriod(int preordersUpdatePeriod) 
    {
        this.preordersUpdatePeriod = preordersUpdatePeriod;
    }

    public int getMaxArchivOrdersCount() {
        return maxArchivOrdersCount;
    }

    public void setMaxArchivOrdersCount(int maxArchivOrdersCount) {
        this.maxArchivOrdersCount = maxArchivOrdersCount;
    }
}
